package com.zy.controller;


import com.zy.constant.MessageConstant;
import com.zy.constant.RedisConstant;
import com.zy.constant.RedisMessageConstant;
import com.zy.entity.Result;
import com.zy.utils.SMSUtils;
import com.zy.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

	@Autowired
	private JedisPool jedisPool;

	@RequestMapping("/send4Order")
	public Result send4Order(String telephone){
		//生成验证码
		Integer yzm = ValidateCodeUtils.generateValidateCode(4);

		try {
			//发送短信
			// SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,yzm.toString());
			System.out.println("手机号为："+telephone+"收到了一条预约信息验证码："+yzm);
			//存储在redis中五分钟 (手机号+业务号)=key   ,重复提交覆盖
			jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,yzm.toString());
			return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
		}

	}

	@RequestMapping("/send4Login")
	public Result send4Login(String telephone){
		try {
			Integer code = ValidateCodeUtils.generateValidateCode(6);
//		SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,code.toString());
			System.out.println("手机号为："+telephone+"收到了一条登入信息验证码："+code);
			jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,300,code.toString());
			return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
		}
	}
}
